package org.op.nav;

import java.awt.HeadlessException;

import javax.swing.JOptionPane;

import org.op.FrameView;
import org.op.service.WwDocumentInfo;
import org.op.service.WwInfoObject;
import org.op.service.config.ConfigurationService;
import org.op.service.fileio.FileRefactoringService;
import org.op.service.log.LoggerService;
import org.op.util.MessageController;

public class FolderControllerImpl implements FolderController {

	private FrameView frameView;
	private MessageController messageController;
	private NavigationController navController;
	private NavigationView navView;
	private FileRefactoringService refactoringService;
	private ConfigurationService configService;
	private LoggerService logger;
	
	public void addFolder() {
		
		try {
			// get the selected node, and check to see if it is a folder
			WwInfoObject parentFolderInfo = navView.getSelectedNodeObject();
			if (parentFolderInfo instanceof WwDocumentInfo) {
				frameView.showExceptionDialog(messageController.getMessage("action.folder.only"));
			} else {
				String label = messageController.getLabel("folder.add");
				String addMessage = messageController.getMessage("enter.folder.name");
				String folderName = JOptionPane.showInputDialog(null, addMessage, label, 1);
				String parentPath = parentFolderInfo.getFilepath();
				if (folderName!=null) {
					refactoringService.addFolder(parentPath, folderName);
					navController.reloadTree();
				}
			}
		} catch (HeadlessException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void renameFolder() {
		// TODO Auto-generated method stub
	}

	public void trashFolder() {
		try {
			// send out a confirmation dialog
			String trashMessage = messageController.getMessage("trash.folder.confirmation");
			int confirm = JOptionPane.showConfirmDialog(frameView.getFrame(), trashMessage);
			
			// move the folder to the trash
			if (JOptionPane.YES_OPTION == confirm) {
				WwInfoObject parentFolderInfo = navView.getSelectedNodeObject();
				String path = parentFolderInfo.getFilepath();
				refactoringService.trashFile(path);
			
				// refresh the nav tree
				navController.reloadTree();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public FrameView getFrameView() {
		return frameView;
	}

	public void setFrameView(FrameView frameView) {
		this.frameView = frameView;
	}

	public MessageController getMessageController() {
		return messageController;
	}

	public void setMessageController(MessageController messageController) {
		this.messageController = messageController;
	}

	public NavigationController getNavController() {
		return navController;
	}

	public void setNavController(NavigationController navController) {
		this.navController = navController;
	}

	public NavigationView getNavView() {
		return navView;
	}

	public void setNavView(NavigationView navView) {
		this.navView = navView;
	}

	public FileRefactoringService getRefactoringService() {
		return refactoringService;
	}

	public void setRefactoringService(FileRefactoringService refactoringService) {
		this.refactoringService = refactoringService;
	}

	public ConfigurationService getConfigService() {
		return configService;
	}

	public void setConfigService(ConfigurationService configService) {
		this.configService = configService;
	}

	public LoggerService getLogger() {
		return logger;
	}

	public void setLogger(LoggerService logger) {
		this.logger = logger;
	}

	

}
